package com.mco.socket.bio;

import cn.hutool.core.util.IdUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author ：liao.wei
 * @date ：2023/10/7 14:43
 * @package : com.mco.socket.read
 */
public class RedisServerBIOMutiThread {
	public static void main(String[] args) throws IOException {
		ServerSocket socketServer = new ServerSocket(6688);
		while (true) {
			System.out.println("模拟Redis server启动 -- 等待连接");
			Socket socket = socketServer.accept();
			System.out.println("--客户端连接成功--");
			new Thread(() -> {
				try {
					InputStream inputStream = socket.getInputStream();
					int lenth = -1;
					byte[] bytes = new byte[1024];
					System.out.println("---等待读取数据---"+ IdUtil.simpleUUID());
					while ((lenth = inputStream.read(bytes)) != -1) {
						System.out.println("---正在读取数据---");
						System.out.println("---数据内容---====>" + new String(bytes, 0, lenth));
						System.out.println();
					}
					
					inputStream.close();
					socket.close();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}, Thread.currentThread().getName()).start();
		}
	}
}
